package org.boyan.modules.cgform.service.impl;

/**
 * @author: zhuxiaobao
 * @time: 2021/9/26 12:32
 */

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.boyan.common.online.api.IOnlineBaseExtAPI;
import org.boyan.common.system.vo.DictModel;
import org.boyan.modules.cgform.service.IOnlCgformHeadService;
import org.boyan.modules.cgform.util.b;
import org.boyan.modules.cgreport.service.IOnlCgreportAPIService;
import org.boyan.modules.config.exception.DBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("onlineBaseExtAPI")
public class i implements IOnlineBaseExtAPI {
    private static final Logger a = LoggerFactory.getLogger(i.class);

    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;

    @Autowired
    IOnlCgreportAPIService onlCgreportAPIService;

    @Override
    public String cgformPostCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        String str = b.a();
        jsonObject.put("id", str);
        this.onlCgformHeadService.addCrazyFormData(tableName, jsonObject);
        return str;
    }

    @Override
    public String cgformPutCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        jsonObject.remove("create_by");
        jsonObject.remove("create_time");
        jsonObject.remove("update_by");
        jsonObject.remove("update_time");
        this.onlCgformHeadService.editCrazyFormData(tableName, jsonObject);
        return jsonObject.getString("id");
    }

    @Override
    public String cgformDeleteDataByCode(String cgformCode, String dataIds) {
        return this.onlCgformHeadService.deleteDataByCode(cgformCode, dataIds);
    }

    @Override
    public JSONObject cgformQueryAllDataByTableName(String tableName, String dataIds) {
        try {
            return this.onlCgformHeadService.queryAllDataByTableNameForDesform(tableName, dataIds);
        } catch (DBException dBException) {
            a.error("查询主子表数据失败：", (Throwable)dBException);
            return null;
        }
    }

    @Override
    public Map<String, Object> cgreportGetData(String code, String forceKey, String dataList) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(forceKey, dataList);
        hashMap.put("getAll", Boolean.TRUE);
        return this.onlCgreportAPIService.getDataByCode(code, hashMap);
    }

    @Override
    public List<DictModel> cgreportGetDataPackage(String code, String dictText, String dictCode, String dataList) {
        String str = "force_" + dictCode;
        Map<String, Object> map = cgreportGetData(code, str, dataList);
        List<Map<String,Object>> list = (List<Map<String,Object>>)map.get("records");
        ArrayList<DictModel> arrayList = new ArrayList<>();
        for (Map<String,Object> map1 : list) {
            String str1 = (String)map1.get(dictCode);
            String str2 = (String)map1.get(dictText);
            arrayList.add(new DictModel(str1, str2));
        }
        return arrayList;
    }
}
